using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameVuTru.ManagerScreens;
using GameVuTru.ResourceManager;
using GameVuTru.Screens;

namespace GameVuTru.GameObject
{
    class CreateLevel
    {
        enum statusMove { BEGIN, MOVE, FINISH }
        public static int level = 1;
        public int FLOOR1 = 40, FLOOR2 = 100, FLOOR3 = 160, FLOOR4 = 220;
        //public int numberEnemy;
        public static List<CObject> listenemy;
        public List<CObject> listenemyleft;
        public List<CObject> listenemyright;
        public List<CObject> listenemytop;
        public List<CObject> listenemybottom;
        public Random rd = new Random();
        public bool change = false;
        public bool checklistleft, checklistright, checklisttop, checklistbottom; //ngung ko chay chuyen dong nua
        statusMove statusmove;

        int speed;//dung khi tat ca chuyen dong giong nhau
        public CreateLevel()
        {
            // level = 1;
            listenemy = new List<CObject>();
            listenemyleft = new List<CObject>();
            listenemyright = new List<CObject>();
            listenemytop = new List<CObject>();
            listenemybottom = new List<CObject>();
            //  numberEnemy = 12;
            change = true;
            statusmove = statusMove.BEGIN;
            speed = 3;
        }
        public void changeLevel()
        {
            checklistleft = false;
            checklistright = false;
            checklisttop = false;
            checklistbottom = false; //ngung ko chay chuyen dong nua
            if (level == 1)
            {
                for (int i = 1; i < 10; i++)
                {
                    Enemy enemy1 = new Enemy(new Vector2(-i * 72, 50), Enemy.EnemyType.SLIVER);
                    //enemy1.AddBonus();
                    Enemy enemy2 = new Enemy(new Vector2(-i * 72, 110), Enemy.EnemyType.SLIVER);
                    //enemy2.AddBonus();
                    listenemy.Add(enemy1);
                    listenemy.Add(enemy2);
                }

            }
            /***************************************************************************************************************************************************/
            else if (level == 2)
            {
                for (int i = 1; i < 10; i++)
                {
                    Enemy enemy1 = new Enemy(new Vector2(i * 70 + 800, 50),  Enemy.EnemyType.SLIVER);
                    Enemy enemy2 = new Enemy(new Vector2(i * 70 + 800, 110),  Enemy.EnemyType.SLIVER);
                    listenemy.Add(enemy1);
                    listenemy.Add(enemy2);
                }

            }
            /***************************************************************************************************************************************************/
            else if (level == 3)
            {
                for (int i = 1; i <= 5; i++)
                {
                    Enemy enemy1 = new Enemy(new Vector2(680, i * 60),  Enemy.EnemyType.SLIVER);
                    Enemy enemy2 = new Enemy(new Vector2(120, i * 60),  Enemy.EnemyType.SLIVER);

                    listenemyleft.Add(enemy1);
                    listenemyright.Add(enemy2);
                    //
                    Enemy enemy3 = new Enemy(new Vector2(605, i * 60),  Enemy.EnemyType.YELLOW);
                    Enemy enemy4 = new Enemy(new Vector2(45, i * 60),  Enemy.EnemyType.YELLOW);
                    listenemyleft.Add(enemy3);
                    listenemyright.Add(enemy4);
                    Enemy enemy5 = new Enemy(new Vector2(605, i * 60),  Enemy.EnemyType.YELLOW);
                    Enemy enemy6 = new Enemy(new Vector2(45, i * 60),  Enemy.EnemyType.YELLOW);
                    listenemyleft.Add(enemy5);
                    listenemyright.Add(enemy6);
                }
                for (int i = 0; i < listenemyleft.Count; i++)
                {
                    listenemy.Add(listenemyleft[i]);
                }
                for (int i = 0; i < listenemyright.Count; i++)
                {
                    listenemy.Add(listenemyright[i]);
                }
            }
            /****************************7****************************************/
            else if (level == 4)
            {
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Enemy enemy1;
                        if (j % 2 == 0)
                        {
                            enemy1 = new Enemy(new Vector2((i * 70) + 35, (-j * 150) - 35),  Enemy.EnemyType.STONE);
                        }
                        else
                        {
                            enemy1 = new Enemy(new Vector2((i * 70) + 450, (-j * 150) - 35),  Enemy.EnemyType.STONE);
                        }

                        listenemy.Add(enemy1);
                    }
                }
            }
            /***************************************************************************************************************************************************/
            else if (level == 5)
            {
                for (int i = 1; i <= 9; i++)
                {
                    Enemy enemy1 = new Enemy(new Vector2(680, -i * 70),  Enemy.EnemyType.SLIVER);
                    Enemy enemy2 = new Enemy(new Vector2(120, -i * 70),  Enemy.EnemyType.RED);
                    Enemy enemy3 = new Enemy(new Vector2(120, -(i + 9) * 70),  Enemy.EnemyType.YELLOW);

                    listenemyleft.Add(enemy1);
                    listenemyright.Add(enemy2);
                    listenemytop.Add(enemy3);
                }
                for (int i = 0; i < listenemyleft.Count; i++)
                {
                    listenemy.Add(listenemyleft[i]);
                }
                for (int i = 0; i < listenemyright.Count; i++)
                {
                    listenemy.Add(listenemyright[i]);
                }
                for (int i = 0; i < listenemytop.Count; i++)
                {
                    listenemy.Add(listenemytop[i]);
                }
            }
            /***************************************************************************************************************************************************/
            else if (level == 6)
            {
                for (int i = 1; i <= 9; i++)
                {
                    Enemy enemy1 = new Enemy(new Vector2(680, 480 + i * 70),  Enemy.EnemyType.YELLOW);
                    Enemy enemy2 = new Enemy(new Vector2(120, 480 + i * 70),  Enemy.EnemyType.YELLOW);
                    Enemy enemy3 = new Enemy(new Vector2(120, (i + 9) * 70 + 480),  Enemy.EnemyType.RED);
                    Enemy enemy4 = new Enemy(new Vector2(680, (i + 9) * 70 + 480),  Enemy.EnemyType.RED);
                    listenemyleft.Add(enemy1);
                    listenemyright.Add(enemy2);
                    listenemytop.Add(enemy3);
                    listenemybottom.Add(enemy4);
                }
                for (int i = 0; i < listenemyleft.Count; i++)
                {
                    listenemy.Add(listenemyleft[i]);
                }
                for (int i = 0; i < listenemyright.Count; i++)
                {
                    listenemy.Add(listenemyright[i]);
                }
                for (int i = 0; i < listenemytop.Count; i++)
                {
                    listenemy.Add(listenemytop[i]);
                }
                for (int i = 0; i < listenemybottom.Count; i++)
                {
                    listenemy.Add(listenemybottom[i]);
                }
            }
            /**************************************************7*************************************************************************************************/
            else if (level == 7)
            {
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Enemy enemy1;
                        if (j % 2 == 0)
                        {
                            enemy1 = new Enemy(new Vector2((i * 240) + 35, (-j * 250) - 35),  Enemy.EnemyType.YELLOW);
                        }
                        else
                        {
                            enemy1 = new Enemy(new Vector2((i * 240) + 155, (-j * 250) - 35),  Enemy.EnemyType.YELLOW);
                        }

                        listenemy.Add(enemy1);
                    }
                }
            }

            /********************************************8******************/
            else if (level == 8)
            {
                for (int i = 3; i < 9; i++)
                {
                    listenemy.Add(new Enemy(new Vector2((i * 70), -70),  Enemy.EnemyType.VITDO));
                    listenemy.Add(new Enemy(new Vector2((i * 70), -130),  Enemy.EnemyType.VITDO));
                    listenemy.Add(new Enemy(new Vector2((i * 70), -190),  Enemy.EnemyType.VITDO));
                    listenemy.Add(new Enemy(new Vector2((i * 70), -250),  Enemy.EnemyType.VITDO));
                }
            }
            else if (level == 9)
            {
                listenemy.Add(new Enemy(new Vector2(400, 50),  Enemy.EnemyType.BOSSRED));
            }

        }
        public void Update(GameTime gametime)
        {
            if (change)
            {
                if (!GameplayScreen.ContinueGame)
                {
                    changeLevel();
                }

                change = false;
                statusmove = statusMove.MOVE;
            }
            if (statusmove == statusMove.MOVE)
            {
                if (level == 1 || level == 2)
                {
                    foreach (Enemy item in listenemy)
                    {
                        if (level == 1)
                        {
                            item.moveRight(3);
                            if (item.postion.X >= 680)
                            {
                                statusmove = statusMove.FINISH;

                            }
                        }
                        else if (level == 2)
                        {
                            item.moveleft(3);
                            if (item.postion.X <= 120)
                            {
                                statusmove = statusMove.FINISH;

                            }
                        }
                    }
                }
                //level 3
                else if (level == 3)
                {
                    for (int i = 0; i < listenemyleft.Count; i++)
                    {
                        listenemy[i].postion.X += listenemy[i].vecloc.X;
                        if (listenemy[i].postion.X <= 35 || listenemy[i].postion.X >= 765)
                        {
                            listenemy[i].vecloc.X *= -1;
                        }
                    }

                    for (int i = listenemyleft.Count; i < listenemy.Count; i++)
                    {

                        listenemy[i].postion.X -= listenemy[i].vecloc.X;
                        if (listenemy[i].postion.X <= 35 || listenemy[i].postion.X >= 765)
                        {
                            listenemy[i].vecloc.X *= -1;
                        }
                    }

                }
                /***************************************LEVEL 4^*****************************************************/
                else if (level == 4)
                {
                    for (int i = 0; i < listenemy.Count; i++)
                    {

                        listenemy[i].postion.Y += 3;
                        Enemy en = (Enemy)listenemy[i];
                        //neu ra khoi man hinh thi chet nhe
                        if (listenemy[i].postion.X <= 0 || listenemy[i].postion.X >= 800 || listenemy[i].postion.Y >= 500)
                        {
                            en.heath -= 500;
                            en.DieBecauseEatBullet = false;
                        }
                    }
                }
                /***************************************************LEVEL 5******************************************************************/
                else if (level == 5)
                {
                    if (!checklistleft)
                    {
                        //nhanh ben fai
                        for (int i = 0; i < listenemyleft.Count; i++)
                        {
                            listenemy[i].postion.Y += listenemy[i].vecloc.Y;
                            if (listenemy[i].postion.Y >= FLOOR3)
                            {
                                listenemy[i].postion.Y = FLOOR3;
                                listenemy[i].postion.X += listenemy[i].vecloc.X;
                            }
                            if (listenemy[listenemyleft.Count - 1].postion.Y >= FLOOR3)
                            {
                                checklistleft = true;
                            }
                        }
                    }
                    if (!checklistright)
                    {
                        //nhanh ben fai
                        for (int i = listenemyleft.Count; i < (listenemyleft.Count + listenemyright.Count); i++)
                        {
                            listenemy[i].postion.Y += listenemy[i].vecloc.Y;
                            if (listenemy[i].postion.Y >= FLOOR2)
                            {
                                listenemy[i].postion.Y = FLOOR2;
                                listenemy[i].postion.X -= listenemy[i].vecloc.X;
                            }
                            if (listenemy[(listenemyleft.Count + listenemyright.Count) - 1].postion.Y >= FLOOR2)
                            {
                                checklistright = true;
                            }
                        }
                    }
                    //nhanh so 3
                    if (!checklisttop)
                    {
                        //nhanh ben fai
                        for (int i = (listenemyleft.Count + listenemyright.Count); i < listenemy.Count; i++)
                        {
                            listenemy[i].postion.Y += listenemy[i].vecloc.Y;
                            if (listenemy[i].postion.Y >= FLOOR1)
                            {
                                listenemy[i].postion.Y = FLOOR1;
                                listenemy[i].postion.X -= listenemy[i].vecloc.X;
                            }
                            if (listenemy[listenemy.Count - 1].postion.Y >= FLOOR1)
                            {
                                checklisttop = true;
                            }
                        }
                    }

                }
                /********************************************LEVEL 6****************************************************************************/
                else if (level == 6)
                {
                    if (!checklistleft)
                    {
                        //nhanh ben fai
                        for (int i = 0; i < listenemyleft.Count; i++)
                        {
                            listenemy[i].postion.Y -= listenemy[i].vecloc.Y;
                            if (listenemy[i].postion.Y <= FLOOR1)
                            {
                                listenemy[i].postion.Y = FLOOR1;
                                listenemy[i].postion.X += listenemy[i].vecloc.X;
                            }
                            if (listenemy[listenemyleft.Count - 1].postion.Y <= FLOOR1)
                            {
                                checklistleft = true;
                            }
                        }
                    }
                    if (!checklistright)
                    {
                        //nhanh ben fai
                        for (int i = listenemyleft.Count; i < (listenemyleft.Count + listenemyright.Count); i++)
                        {
                            listenemy[i].postion.Y -= listenemy[i].vecloc.Y;
                            if (listenemy[i].postion.Y <= FLOOR2)
                            {
                                listenemy[i].postion.Y = FLOOR2;
                                listenemy[i].postion.X -= listenemy[i].vecloc.X;
                            }
                            if (listenemy[(listenemyleft.Count + listenemyright.Count) - 1].postion.Y <= FLOOR2)
                            {
                                checklistright = true;
                            }
                        }
                    }
                    //   nhanh so 3
                    if (!checklisttop)
                    {
                        //nhanh ben fai
                        for (int i = (listenemyleft.Count + listenemyright.Count); i < (listenemyleft.Count + listenemyright.Count + listenemytop.Count); i++)
                        {
                            listenemy[i].postion.Y -= listenemy[i].vecloc.Y;
                            if (listenemy[i].postion.Y <= FLOOR3)
                            {
                                listenemy[i].postion.Y = FLOOR3;
                                listenemy[i].postion.X -= listenemy[i].vecloc.X;
                            }
                            if (listenemy[(listenemyleft.Count + listenemyright.Count + listenemytop.Count) - 1].postion.Y <= FLOOR3)
                            {
                                checklisttop = true;
                            }
                        }
                    }
                    if (!checklistbottom)
                    {

                        for (int i = (listenemyleft.Count + listenemyright.Count + listenemytop.Count); i < listenemy.Count; i++)
                        {
                            listenemy[i].postion.Y -= listenemy[i].vecloc.Y;
                            if (listenemy[i].postion.Y <= FLOOR4)
                            {
                                listenemy[i].postion.Y = FLOOR4;
                                listenemy[i].postion.X += listenemy[i].vecloc.X;
                            }
                            if (listenemy[listenemy.Count - 1].postion.Y <= FLOOR4)
                            {
                                checklistbottom = true;
                            }
                        }
                    }

                }
                /***************************************LEVEL 7^*****************************************************/
                else if (level == 7)
                {
                    for (int i = 0; i < listenemy.Count; i++)
                    {

                        listenemy[i].postion.Y += 3;
                        Enemy en = (Enemy)listenemy[i];
                        //neu ra khoi man hinh thi chet nhe
                        if (listenemy[i].postion.X <= 0 || listenemy[i].postion.X >= 800 || listenemy[i].postion.Y >= 500)
                        {
                            en.heath -= 500;
                            en.DieBecauseEatBullet = false;
                        }
                    }
                }



                /********************************************LEVEL  8****************************************************************/
                else if (level == 8)
                {
                    for (int i = 0; i < listenemy.Count; i++)
                    {
                        if (!checklistbottom)
                        {
                            listenemy[i].postion.Y += 3;

                            if (listenemy[listenemy.Count - 1].postion.Y >= FLOOR1)
                            {
                                checklistbottom = true;
                            }
                        }
                        if (checklistbottom)
                        {

                            listenemy[i].postion.X += speed;
                            if (listenemy[i].postion.X <= 35 || listenemy[i].postion.X >= 765)
                            {
                                speed *= -1;
                            }
                        }

                    }
                }

            /********************************************LEVEL  9****************************************************************/
                else if (level == 9)
                {
                    for (int i = 0; i < listenemy.Count; i++)
                    {
                        if (!checklistbottom)
                        {
                            listenemy[i].postion.Y += 3;
                            if (listenemy[i].postion.Y >= FLOOR4)
                            {
                                checklistbottom = true;
                            }
                        }
                        else
                        {
                            listenemy[i].postion += listenemy[i].vecloc;
                            if (listenemy[i].postion.X <= 70 || listenemy[i].postion.X >= 730)
                            {
                                listenemy[i].vecloc.X *= -1;
                            }
                            if (listenemy[i].postion.Y <= 70 || listenemy[i].postion.Y >= 400)
                            {
                                listenemy[i].vecloc.Y *= -1;
                            }
                        }
                    }
                }

            }

            /*********************************UPDATE CHINH**********************************************************************/
            foreach (CObject item in listenemy)
            {
                Enemy en = (Enemy)item;
                //ban dan khi nam trong man hinh va khac STONE
                if (en.isvisible && en.postion.X >= 35 && en.postion.X <= 765 && en.postion.Y >= 0 && en.postion.Y <= 460 && en.m_typeenemy != Enemy.EnemyType.STONE)
                {
                    int shoot;
                    if (level < 4)
                    {
                        shoot = rd.Next(0, 1000 / level);
                    }
                    //man boss
                    else if (level == 9)
                    {
                        shoot = rd.Next(0, 100);
                    }
                    else
                    {
                        shoot = rd.Next(0, 1000 / 3);
                    }

                    if (shoot == 5)
                    {
                        en.AddBullet();
                    }
                }

                item.Update(gametime);
            }



        }

        public void Draw(SpriteBatch sp)
        {
            foreach (CObject item in listenemy)
            {
                item.Draw(sp);
            }
        }
    }
}
